package view
{
	import alttackEvents.AlttackEvent;
	
	import com.greensock.easing.Cubic;
	import com.greensock.greensock.TweenLite;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	import flash.utils.setTimeout;
	
	import model.DataBar;
	import model.Image;
	
	public class Toolbar extends Sprite
	{
		public var trigger:Boolean = true;
		private var iconArr:Array=[];
		private var cannon:Cannon;
		private var image1:Image;
		private var image2:Image;
		private var image3:Image;
		private var clickPigGame:Sprite;						
		public static const COMPLETE:String = "complete";
		public function Toolbar()
		{
			super();						
			this.addEventListener(Event.ADDED_TO_STAGE,oninit)
				
		}
		
		protected function oninit(event:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE,oninit)
			drawClickRect();
			image1 = new Image("assets/牛皮底栏.png");
			image2 = new Image("assets/底栏数字.png");
			image3 = new Image("assets/炮座.png");
			
			iconArr.push(image1,image2,image3);
			
			image1.addEventListener(Image.COMPLETE,onComplete);	
			image2.addEventListener(Image.COMPLETE,onComplete);	
			image3.addEventListener(Image.COMPLETE,onComplete);	
			addCannon();							
		}
		
		/**
		 *画一个响应点击的方块区域 
		 * 
		 */		
		private function drawClickRect():void
		{
			clickPigGame = new Sprite();
			clickPigGame.graphics.beginFill(0xffffff,0);
			clickPigGame.graphics.drawRect(0,0,DataBar.gameW,DataBar.rectH);
			clickPigGame.graphics.endFill();			
			addChild(clickPigGame);
			
		}

	
		/**
		 * 统计加载,完成后排列图标
		 */		
		
		private var counting:uint=0;
		private function onComplete(event:Event):void
		{
			counting ++;
			event.currentTarget.removeEventListener(Image.COMPLETE,onComplete);
			if(counting == 4)
			{
				arrange();
			}
			
		}
		/**
		 *排列图标布局</p>
		 * 排列图标完成后，拿出大炮
		 * 
		 */		
		
		private function arrange():void
		{
			for(var i:uint=0; i<iconArr.length; i++)
			{				
				addChild(iconArr[i])
				iconArr[i].y = DataBar.rectH;
			}
			iconArr[2].x = (iconArr[0].width - iconArr[2].width) / 2;				
			arrangeCannon();										
		}
		/**
		 * 添加大炮到显示列表,并调整大炮的位置。</p>
		 * 然后这个模块从下方滑出
		 * 
		 */		
		
		private function arrangeCannon():void
		{
			this.addChild(cannon);									
			cannon.x = iconArr[0].width / 2;		
			cannon.y = DataBar.rectH + iconArr[0].height/2;
			location();									
		}
		/**
		 * 模块从下方滑出</p>
		 * 然后timer开始cannon跟随鼠标转动
		 */		
		
		private var num:uint;
		private function location():void
		{
			num = 800 - height;			
			x = 0;
			y = 150 + DataBar.titleH;			
			TweenLite.to(this, 0.3, {y:num, ease:Cubic.easeOut});
			setTimeout(collimationCannon,300);
				
		}
		/**
		 *隐藏这个模块 ,并从父显示列表删除。
		 * 
		 */		
		public function meHide():void
		{			
			TweenLite.to(this, 0.3, {y:150 + DataBar.titleH, ease:Cubic.easeOut});	
			setTimeout(removeMe,600);
			
		}		
		private function removeMe():void
		{						
			if(parent)
			{
				parent.removeChild(this);			
			}
		}
		
		//加载大炮
		private function addCannon():void
		{
			cannon = new Cannon();			
			cannon.addEventListener(Image.COMPLETE,onComplete);
		}				
	
		
		
		//大炮瞄准
		private var dX:Number;
		private var dY:Number;
		private var angle:Number;
		private var timer:Timer;
		/**
		 * 开始瞄准并可以开炮</p>
		 * 侦听攻击区域的点击；
		 * 保存炮的坐标；
		 * 
		 */		
		private function collimationCannon():void
		{		
			clickPigGame.addEventListener(MouseEvent.CLICK,attack);
			DataBar.cannonPoint = localToGlobal(new Point(cannon.x,cannon.y));
			
			timer = new Timer(40);
			timer.addEventListener(TimerEvent.TIMER,onTimer);
			timer.start();							
		}
		
		protected function onTimer(event:TimerEvent):void
		{
			if(trigger && stage)
			{			
				dX = stage.mouseX - DataBar.cannonPoint.x;			
				dY = stage.mouseY - DataBar.cannonPoint.y;			
				angle = Math.atan2(dY,dX);				
				cannon.rotation = angle / Math.PI * 180 + 90;
			}						
		}
		
		//大炮攻击
		public function attack(event:MouseEvent):void
		{
			if(trigger)
			{
				cannon.onFire();
				bullet(angle);								
			}
			
		}
		
		private var vX:Number;
		private var vY:Number;
		public var alttacks:AlttackEvent;
		/**
		 *攻击发出子弹事件 ，包含子弹初始坐标。
		 * @param ang
		 * 
		 */		
		private function bullet(ang:Number):void
		{
						
			vX = Math.cos(ang) * DataBar.cannonHeight + DataBar.cannonPoint.x;			
			vY = Math.sin(ang) * DataBar.cannonHeight + DataBar.cannonPoint.y;			
			alttacks = new AlttackEvent(AlttackEvent.ALTTACK,new Point(vX,vY))
			dispatchEvent(alttacks);
			trace(alttacks)
//			bulletPoint = globalToLocal(bulletPoint);
//			var bullet:Bullet = new Bullet();
//			addChild(bullet);
//			bullet.x = bulletPoint.x;
//			bullet.y = bulletPoint.y;
			
		}
		
		
	}
}